package com.zjh.mailbox.contorller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.yaowk.authc.model.User;
import com.yaowk.common.kit.Constant;
import com.yaowk.common.kit.I18nKit;
import com.yaowk.common.kit.ResultKit;
import com.zjh.mailbox.model.Mailbox;
import com.zjh.mailbox.model.Platform;
import com.zjh.mailbox.model.page.MailboxPage;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by zZz on 2017/5/16 0016.
 */
public class MailboxController extends Controller {

    /**
     * 添加信箱内容
     *
     * @param title|标题|String|必填
     * @param accessory|附件|String|选填
     * @param content|内容|String|必填
      * @title 添加信箱内容
     */
    @RequiresPermissions("mailbox:update")
    public void addMail()
    {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Mailbox mailbox = new Mailbox();
        boolean result = mailbox.setTitle(getPara("title"))
                .setAccessory(getPara("accessory"))
                .setContent(getPara("content"))
                .setCreateUser(user.getId())
                .setCreateTime(new java.sql.Date(new java.util.Date().getTime()))
                .setPlatformId(user.getPlatformId())
                .save();
        if(result)
        {
            renderJson(ResultKit.success(I18nKit.get("add_success", Constant.MAILBOX_MODULE, this)));
        }
        else
        {
            renderJson(ResultKit.fail(I18nKit.get("add_fail", Constant.MAILBOX_MODULE, this)));
        }
    }

    /**
     * 信箱内容审核通过
     * @param id|信箱id|Integer|必填
     * @param handle_user|处理人|Integer|必填
     * @title 信箱内容审核通过
     */
    @RequiresPermissions("mailbox:update")
    public void passMail()
    {
        Mailbox mailbox = Mailbox.dao.findById(getParaToInt("id"));
        com.yaowk.authc.model.User user = (com.yaowk.authc.model.User) SecurityUtils.getSubject().getPrincipal();
        boolean result = mailbox.setAuditStatus(1)
                .setAuditUser(user.getId())
                .setHandleUser(getParaToInt("handle_user"))
                .setHandleStatus(1)
                .update();
        if(result)
        {
            renderJson(ResultKit.success(I18nKit.get("audit_success", Constant.MAILBOX_MODULE, this)));
        }
        else
        {
            renderJson(ResultKit.fail(I18nKit.get("audit_fail", Constant.MAILBOX_MODULE, this)));
        }
    }

    /**
     * 信箱内容审核不通过
     * @param id|信箱id|Integer|必填
     * @title 信箱内容审核不通过
     */
    @RequiresPermissions("mailbox:update")
    public void noPassMail()
    {
        Mailbox mailbox = Mailbox.dao.findById(getParaToInt("id"));
        com.yaowk.authc.model.User user = (com.yaowk.authc.model.User) SecurityUtils.getSubject().getPrincipal();
        boolean result = mailbox.setAuditStatus(-1)
                .setAuditUser(user.getId())
                .update();
        if(result)
        {
            renderJson(ResultKit.success(I18nKit.get("audit_no_pass", Constant.MAILBOX_MODULE, this)));
        }
        else
        {
            renderJson(ResultKit.fail(I18nKit.get("audit_fail", Constant.MAILBOX_MODULE, this)));
        }
    }

    /**
     * 信箱内容处理完毕
     * @param id|信箱id|Integer|必填
     * @title 信箱内容处理完毕
     */
    @RequiresPermissions("mailbox:update")
    public void finishMail()
    {
        Mailbox mailbox = Mailbox.dao.findById(getParaToInt("id"));
        if(mailbox.getHandleStatus() == 0)
        {
            renderJson(ResultKit.fail(I18nKit.get("can_not_finish", Constant.MAILBOX_MODULE, this)));
            return;
        }
        if(mailbox.getHandleStatus() == 1)
        {
            boolean result = mailbox.setHandleStatus(2)
                    .update();
            if(result)
            {
                renderJson(ResultKit.success(I18nKit.get("finish_success", Constant.MAILBOX_MODULE, this)));
            }
            else
            {
                renderJson(ResultKit.fail(I18nKit.get("finish_fail", Constant.MAILBOX_MODULE, this)));
            }
        }

    }

    /**
     * 获取可处理人
     * @param moduleid|平台id|Integer|必填
     * @title 获取可处理人
     */
    @RequiresPermissions("mailbox:read")
    public void getHandlers()
    {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<HashMap> ResultList = new ArrayList();
        List<Platform> PlatformList = Platform.dao.getHandlers(getParaToInt("moduleid"),user.getPlatformId());
        HashMap<String,Object> tempMap = new HashMap();
        for(Platform p : PlatformList)
        {
            if(tempMap.containsKey("name"))
            {
                if(tempMap.get("name").toString() == p.get("platformname").toString() || tempMap.get("name").toString().equals(p.get("platformname").toString()) )
                {
                    List<HashMap<String,Object>> l = (List<HashMap<String, Object>>) tempMap.get("handlers");
                    HashMap<String,Object> userMap = new HashMap<>();
                    userMap.put("name",p.get("username"));
                    userMap.put("id",p.get("userid"));
                    l.add(userMap);
                }
                else
                {
                    tempMap = new HashMap<String,Object>();
                    tempMap.put("name", p.get("platformname"));
                    List<HashMap<String,Object>> l = new ArrayList<>();
                    HashMap<String,Object> userMap = new HashMap<>();
                    userMap.put("name",p.get("username"));
                    userMap.put("id",p.get("userid"));
                    l.add(userMap);
                    tempMap.put("handlers",l);
                    ResultList.add(tempMap);
                }
            }
            else
            {
                tempMap.put("name", p.get("platformname"));
                List<HashMap<String,Object>> l = new ArrayList<>();
                HashMap<String,Object> userMap = new HashMap<>();
                userMap.put("name",p.get("username"));
                userMap.put("id",p.get("userid"));
                l.add(userMap);
                tempMap.put("handlers",l);
                ResultList.add(tempMap);
            }
        }
        renderJson(ResultKit.success(ResultList));
    }

    /**
     * 获取信箱列表
     *
     * @param beginTime|开始时间|String|选填
     * @param endTime|结束时间|String|选填
     * @param title|标题|String|选填
     * @param auditStatus|审核状态（0：待审核；1：审核通过；2：审核不通过）|Integer|选填
     * @param handleStatus|处理状态（0：未处理（可不显示）；1：处理中；2：处理完毕）|Integer|选填
     * @param pageSize|每页条数|Integer|是
     * @param pageNumber|当前页|Integer|是
     * @title 获取信箱列表
     */
    @RequiresPermissions("mailbox:read")
    public void getMailboxList()
    {
        /*
        MailboxPage newsPage = getBean(MailboxPage.class, "");
        Page<Mailbox> page = Mailbox.dao.paginate(newsPage);

        renderJson(ResultKit.success(page));
        */
        getMailboxListByPlatform();
    }

    /**
     * 获取信箱列表（处理人）
     *
     * @param beginTime|开始时间|String|选填
     * @param endTime|结束时间|String|选填
     * @param title|标题|String|选填
     * @param auditStatus|审核状态（0：待审核；1：审核通过；2：审核不通过）|Integer|选填
     * @param handleStatus|处理状态（0：未处理（可不显示）；1：处理中；2：处理完毕）|Integer|选填
     * @param pageSize|每页条数|Integer|是
     * @param pageNumber|当前页|Integer|是
     * @title 获取信箱列表（处理人）
     */
    @RequiresPermissions("mailbox:read")
    public void getMailboxListByHandleuser()
    {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        MailboxPage newsPage = getBean(MailboxPage.class, "");
        newsPage.setHandle_user(user.getId().toString());
        Page<Mailbox> page = Mailbox.dao.paginate(newsPage);

        renderJson(ResultKit.success(page));
    }

    /**
     * 获取信箱列表（平台）
     *
     * @param beginTime|开始时间|String|选填
     * @param endTime|结束时间|String|选填
     * @param title|标题|String|选填
     * @param auditStatus|审核状态（0：待审核；1：审核通过；2：审核不通过）|Integer|选填
     * @param handleStatus|处理状态（0：未处理（可不显示）；1：处理中；2：处理完毕）|Integer|选填
     * @param pageSize|每页条数|Integer|是
     * @param pageNumber|当前页|Integer|是
     * @title 获取信箱列表（平台）
     */
    @RequiresPermissions("mailbox:read")
    public void getMailboxListByPlatform()
    {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        MailboxPage newsPage = getBean(MailboxPage.class, "");
        newsPage.setPlatformId(user.getPlatformId().toString());
        Page<Mailbox> page = Mailbox.dao.paginate(newsPage);
        renderJson(ResultKit.success(page));
    }


}
